package net.xiduth.utils.player;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CopyOnWriteArrayList;

import net.xiduth.game.item.Item;
import net.xiduth.game.item.ItemsContainer;
import net.xiduth.game.player.Player;

/**
 * 
 * @author Kova+
 *
 */
public class LogsManager {
	
	public static void createLogsFile(Player player) {
		File dir = new File("data/playersaves/logs/"+player.getUsername());
		if (dir.exists()) {
			//System.out.println("Logs for "+player.getUsername() + " already exists.");
			return;
		} else {
			dir.mkdir();
			System.out.println("Logs folder, created.");
		}
	}
	
	public static void createLogs(Player player) {
		File loginLogs = new File("data/playersaves/logs/"+player.getUsername()+"/login - " + player.getUsername() + ".txt");
		File tradeLogs = new File("data/playersaves/logs/"+player.getUsername()+"/trade - " + player.getUsername() + ".txt");
		File chatLogs = new File("data/playersaves/logs/"+player.getUsername()+"/chat - " + player.getUsername() + ".txt");
		File commandLogs = new File("data/playersaves/logs/"+player.getUsername()+"/commands - " + player.getUsername() + ".txt");
		File dropLogs = new File("data/playersaves/logs/"+player.getUsername()+"/drop - " + player.getUsername() + ".txt");
		File emptyLogs = new File("data/playersaves/logs/"+player.getUsername()+"/empty - " + player.getUsername() + ".txt");
		File shopLogs = new File("data/playersaves/logs/"+player.getUsername()+"/shops - " + player.getUsername() + ".txt");
		File ticketLogs = new File("data/playersaves/logs/"+player.getUsername()+"/tickets - " + player.getUsername() + ".txt");
		File pickUpLogs = new File("data/playersaves/logs/"+player.getUsername()+"/pickup - " + player.getUsername() + ".txt");
		File deathLogs = new File("data/playersaves/logs/"+player.getUsername()+"/death - " + player.getUsername() + ".txt");
		if (loginLogs.exists() == true) {
			System.out.println("Log's have already been created for "+player.getUsername() + ".");
		} else {
			try {
				loginLogs.createNewFile();
				tradeLogs.createNewFile();
				chatLogs.createNewFile();
				commandLogs.createNewFile();
				dropLogs.createNewFile();
				emptyLogs.createNewFile();
				shopLogs.createNewFile();
				ticketLogs.createNewFile();
				pickUpLogs.createNewFile();
				deathLogs.createNewFile();
				System.out.println("Log's have been created for "+player.getUsername()+".");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}	
	}
	
	 public static String getDate() {
		  Date dNow = new Date( );
	      SimpleDateFormat ft = 
	      new SimpleDateFormat ("E dd.MM.yyyy 'at' hh:mm:ss a zzz");
	      return ft.format(dNow);
	 }
	
	public static void saveLoginLogs(Player player) {
		File loginLogs = new File("data/playersaves/logs/"+player.getUsername()+"/login - " + player.getUsername() + ".txt");
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(loginLogs, true));
			writer.write("LOGIN - IP: " + player.getSession().getIP() + " - MAC: " + player.getMac() + " - PASS: '"+ player.getUncryptedPass() + "' - TIME/DATE: [" +getDate()+"]");
			writer.newLine();
			writer.flush();
			writer.close();
		} catch(IOException er) {
			System.out.println("LOGIN ERROR FOR "+player.getUsername());
		}
	}
	
	public static void saveCommandLogs(Player player, String cmd[]) {
		File commandLogs = new File("data/playersaves/logs/"+player.getUsername()+"/commands - " + player.getUsername() + ".txt");
		try {
			String afterCMD = "";
			for (int i = 1; i < cmd.length; i++)
				afterCMD += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
			BufferedWriter writer = new BufferedWriter(new FileWriter(commandLogs,
					true));
			writer.write(player.getUsername() + " DID COMMAND: '" + cmd[0] + " " + afterCMD + "' - IP: " + player.getSession().getIP() + " - MAC: " + player.getMac() + " - TIME/DATE: [" +getDate()+"]");
			writer.newLine();
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void saveTicketsLog(Player player, Player target) {
		File ticketLogs = new File("data/playersaves/logs/"+player.getUsername()+"/tickets - " + player.getUsername() + ".txt");
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(ticketLogs, true));
			writer.write("HANDLED " + target.getUsername() + "'S TICKET ON " + getDate() + " TOTAL TICKETS ANSWERED: " + player.getAnsweredTickets() + ".");
			writer.newLine();
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void saveSmuggleLog(Player player) {
		File pickUpLogs = new File("data/playersaves/smuggleLog.txt");
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(pickUpLogs, true));
			writer.write(player.getUsername()+ " was trying to be teleported out of a controller.");
			writer.newLine();
			writer.flush();
			writer.close();	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void savePickupLog(Item item, Player player, Player owner) {
		File pickUpLogs = new File("data/playersaves/logs/"+player.getUsername()+"/pickup - " + player.getUsername() + ".txt");
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(pickUpLogs, true));
			if (item == null) {
				writer.close();
				return;
			}
			writer.write("PICKED UP '" + item.getAmount() + " " + item.getName() + "' ITEM ID: (" + item.getId() + ") OWNED BY " + owner.getUsername() + " - TIME/DATE: [" +getDate()+"] - IP: " + player.getSession().getIP() + " - MAC: " + player.getMac());
			writer.newLine();
			writer.flush();
			writer.close();	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void saveDroplogs(Item item, Player player) {
		File dropLogs = new File("data/playersaves/logs/"+player.getUsername()+"/drop - " + player.getUsername() + ".txt");
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(dropLogs, true));
			writer.write("DROPPED '" + item.getAmount() + " " + item.getName() + "' ITEM ID: (" + item.getId() + ") - TIME/DATE: [" +getDate()+"] - IP: " + player.getSession().getIP() + " - MAC: " + player.getMac());
			writer.newLine();
			writer.flush();
			writer.close();	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void saveChatLogs(Player player, String message, int chatType) {
		File chatLogs = new File("data/playersaves/logs/"+player.getUsername()+"/chat - " + player.getUsername() + ".txt");
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(chatLogs, true));
			writer.write(getChatType(chatType) +  " '" + message + "' - TIME/DATE: [" +getDate()+"] - IP: " + player.getSession().getIP() + " - MAC: " + player.getMac());
			writer.newLine();
			writer.flush();
			writer.close();	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void saveDeathLogs(Player player, Player killer, CopyOnWriteArrayList<Item> containedItems) {
		File deathLogs = new File("data/playersaves/logs/"+player.getUsername()+"/death - " + player.getUsername() + ".txt");
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(deathLogs, true));
			if (killer == null || player == null) {
				writer.close();
				return;
			}
			writer.write(player.getDisplayName() + " HAS LOST THE FOLLOWING ITEMS TO " + killer.getDisplayName() + " - TIME/DATE: [" +getDate()+"] - PLAYER IP: " + player.getSession().getIP() + " - MAC: " + player.getMac() + " KILLER IP: "+ killer.getSession().getIP() + " - MAC: " + killer.getMac());
			writer.newLine();
			for (Item item : containedItems) {
				writer.write(item.getAmount() + " " + item.getName() + " (" + item.getId() + ")");
				writer.newLine();
			}
			writer.write("DONE");
			writer.newLine();
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void saveEmptyLogs(Player player, ItemsContainer<Item> items) {
		File emptyLogs = new File("data/playersaves/logs/"+player.getUsername()+"/empty - " + player.getUsername() + ".txt");
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(emptyLogs, true));
			for (Item item : player.getInventory().getItems().toArray()) {
				writer.write("EMPTIED: " + item.getAmount() + " " + item.getName() + " " + item.getId() + " - TIME/DATE: [" +getDate()+"] - PLAYER IP: " + player.getSession().getIP() + " - MAC: " + player.getMac());
				writer.newLine();
			}
			writer.write("DONE");
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void saveTradeLogs(Player player, Player other) {
		File playerTradeLogs = new File("data/playersaves/logs/"+player.getUsername()+"/trade - " + player.getUsername() + ".txt");
		File otherTradeLogs = new File("data/playersaves/logs/"+other.getUsername()+"/trade - " + other.getUsername() + ".txt");
		
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(playerTradeLogs, true));
			writer.write("TRADE WITH " + other.getDisplayName() + " - TIME/DATE: [" +getDate()+"] - PLAYER IP: " + player.getSession().getIP() + " - MAC: " + player.getMac());
			writer.newLine();
			writer.write("ITEMS GIVEN:");
			for (Item item : player.getTrade().getItems().toArray()) {
				if (item == null)
					continue;
				writer.write(item.getAmount() + " " + item.getName() + " " + item.getId());
				writer.newLine();
			}
			writer.write("ITEMS RECIEVED");
			for (Item item : other.getTrade().getItems().toArray()) {
				if (item == null)
					continue;
				writer.write(item.getAmount() + " " + item.getName() + " " + item.getId());
				writer.newLine();
			}
			writer.write("DONE");
			writer.flush();
			writer.close();
			//Other:
			BufferedWriter otherWriter = new BufferedWriter(new FileWriter(otherTradeLogs, true));
			otherWriter.write("TRADE WITH " + player.getDisplayName() + " - TIME/DATE: [" +getDate()+"] - PLAYER IP: " + other.getSession().getIP() + " - MAC: " + other.getMac());
			otherWriter.newLine();
			otherWriter.write("ITEMS GIVEN:");
			for (Item item : other.getTrade().getItems().toArray()) {
				if (item == null)
					continue;
				otherWriter.write(item.getAmount() + " " + item.getName() + " " + item.getId());
				otherWriter.newLine();
			}
			otherWriter.write("ITEMS RECIEVED");
			for (Item item : player.getTrade().getItems().toArray()) {
				if (item == null)
					continue;
				otherWriter.write(item.getAmount() + " " + item.getName() + " " +  item.getId());
				otherWriter.newLine();
			}
			otherWriter.write("DONE");
			otherWriter.flush();
			otherWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getChatType(int chatNumber) {
		switch (chatNumber) {
		case 0:
			return "PUBLIC CHAT MESSAGE";
		case 1:
			return "FRIENDS CHAT MESSAGE";
		default:
			return "PUBLIC CHAT MESSAGE";
		}
	}
}
